﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlackBird.Models;
using BlackBird.DataAccess;
using System.Net;
using BlackBird.ControllerHelpers;

namespace BlackBird.Controllers
{
	public partial class ProjectController : Controller
    {
        //
        // GET: /Project/

		public virtual ActionResult Index()
        {
            return View();
        }

		[HttpGet]
		public virtual ActionResult NewProject()
		{
			NewProjectDM npDM = new NewProjectDM();
			return PartialView(MVC.Project.Views.NewProject,npDM);
		}

		[HttpPost]
		public virtual ActionResult NewProject(NewProjectDM dm)
		{
            dm.UserName = User.Identity.Name;
            var p = ProjectDataAccess.NewProject(dm);

			return RedirectToAction(MVC.Home.Index());
		}

		/// <summary>
		/// 返回包括比价单，收入和支出的一个列表view。 在客户端通过 filter来进行
		/// 过滤
		/// </summary>
		/// <param name="type"></param>
		/// <returns></returns>
		public virtual ActionResult ItemListView(SupportTypes types,string projectID)
		{
			ItemListViewDM itemsDM = ProjectHelper.GetProjectItemListViewDM(int.Parse(projectID));
			itemsDM.CheckedFilters.Add(types);

			return View(MVC.Project.Views.ItemListView, itemsDM);
		}

		/// <summary>
		/// 返回比价单，收入，支出，事件等的详细试图，根据状态，决定是否支持审核，或者归档
		/// </summary>
		/// <param name="Id"></param>
		/// <param name="type"></param>
		/// <returns></returns>
		public virtual ActionResult ItemDetailView(String Id,SupportTypes type)
		{
			switch (type)
			{
				case SupportTypes.BiJiaDan:
					{
                        //GetBiJiaDan里包含权限检测
                        return RedirectToAction(MVC.BiJiaDan.GetBiJiaDan(Id));
					}
				case SupportTypes.Expend:
					{
						var data = ProjectHelper.GetIncomeAndExpendById(Id);
						data.type = type;
						return View(MVC.Project.Views.IncomeExpendView, data);
					}
				case SupportTypes.Income:
					{
						var data = ProjectHelper.GetIncomeAndExpendById(Id);
						data.type = type;
						return View(MVC.Project.Views.IncomeExpendView, data);
					}
				case SupportTypes.Event:
					{
						var data = ProjectHelper.GetProjectEventById(Id);
						return View(MVC.Project.Views.ProjectEvent, data);
					}
                case SupportTypes.FukuanDan:
                    {
                        return RedirectToAction(MVC.FuKuanDan.Details(Id));
                    }
				default:
					return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest);
			}

		}

		/// <summary>
		/// 返回审核partial页面
		/// </summary>
		/// <param name="Id"></param>
		/// <param name="type"></param>
		/// <returns></returns>
		public virtual ActionResult ShenHePartialView(String Id, SupportTypes type)
		{
			CommentDM dm = MockupDataStore.GetEmptyCommentDM(Id, type);

			return PartialView(MVC.Shared.Views.Project.ShenHeCommentForm, dm);
		}

		/// <summary>
		/// 提交审核结果
		/// </summary>
		/// <param name="dm"></param>
		/// <returns></returns>
		[HttpPost]
		public virtual ActionResult ShenHeSubmit(CommentDM dm)
		{
			return new HttpStatusCodeResult((int)HttpStatusCode.OK);
		}

		/// <summary>
		/// 返回新加计划view
		/// </summary>
		/// <returns></returns>
		[HttpGet]
		public virtual ActionResult AddPlanPartialView(string ParentId)
		{
			PlanViewItemDM pDm = MockupDataStore.GetEmptyPlanViewItemDM(ParentId);

			return PartialView(MVC.Shared.Views.Plan.NewPlanView, pDm);
		}

		[HttpPost]
		public virtual ActionResult AddProjectPlan(PlanViewItemDM pdm)
		{
			pdm.FinishTimeString = pdm.FinishTime.ToString("yyyy-MM-dd");

			//Insert plan items to database.
			{
				IncomeAndExpendInputDM dm = pdm.GetIncomdeAndExpendObject();
				var currentuserid = AccountHelper.GetCurrentUserId(this);
				dm.Status = SupportTypeStatus.JiHua;
				int eventid = EventsDataAccess.InsertNewEvent(dm, currentuserid);

				pdm.Id = eventid.ToString();
			}

			return Json(pdm, JsonRequestBehavior.AllowGet);
		}

		/// <summary>
		/// 项目计划页面
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public virtual ActionResult PlanProject(string ParentId)
		{
			PlanViewDM pvDM = new PlanViewDM();
			pvDM.ParentId = ParentId;
			pvDM.NewPlanView = MockupDataStore.GetEmptyPlanViewItemDM(ParentId);
			pvDM.DateToItems = EventsHelper.GetProjectPlanView(ParentId);
			return View(MVC.Project.Views.ProjectPlan, pvDM);
		}

		/// <summary>
		/// 删除项目计划的一项
		/// </summary>
		/// <param name="planId"></param>
		/// <returns></returns>
		public virtual ActionResult DeleteProjectPlan(string planId)
		{
			return new HttpStatusCodeResult((int)HttpStatusCode.OK);
		}

		/// <summary>
		/// 提交项目计划，当项目的计划被提交后，则该项目计划以后只能被预览。
		/// 根据项目的执行情况，在查看项目计划时加入项目运行时的收入，支出，实际运行事件。
		/// </summary>
		/// <param name="ParentId"></param>
		/// <returns></returns>
		public virtual ActionResult SubmitProjectPlan(string ParentId)
		{
			return RedirectToAction(MVC.Home.Index());
		}

    }
}
